import {
  HOST,
} from '../constant'
export function getImagePath(path) {
  if (!path) {
    return ''
  }
  if (path.includes('https://')) {
    return path
  }
  if (path.indexOf(HOST.ASSETS_VIEW) === 0) {
    return `${HOST.BASE_URL}${path}`
  }
  return path
}
export function getVideoPath(path) {
  if (!path) {
    return ''
  }
  if (path.includes('https://')) {
    return path
  }
  if (path.indexOf(HOST.ASSETS_VIEW) === 0) {
    return `${HOST.BASE_URL}${path}`
  }
  return path
}
export function getDocPath(path) {
  if (!path) {
    return ''
  }
  if (path.includes('https://')) {
    return path
  }
  if (path.indexOf(HOST.ASSETS_VIEW) === 0) {
    return `${HOST.BASE_URL}${path}`
  }
  return path
}

export function formatPhone(phone) {
  if (!phone) {
    return ''
  }
  return `${phone.substring(0, 3)}****${phone.substring(7, 11)}`
}

export function fixedDecimal(value, precision) {
  precision = precision || 0;
  return parseFloat((parseFloat(value) || 0).toFixed(precision))
}

export function getCarbonReductionByPower(power) {
  // 1度电约减排0.997kg CO2 —— 中国碳交易网
  return fixedDecimal((power * 0.997) / 1000, 2)
}
export function formatNumber(number) {
  // if (number >= 100000000) {
  //   return `${fixedDecimal(number / 100000000, 1)}亿`
  // }
  // if (number >= 10000) {
  //   return `${fixedDecimal(number / 10000, 1)}万`
  // }
  return fixedDecimal(number, 1)
}

export function getAnnualNumber(valPerHour) {
  return formatNumber(valPerHour * 365 * 24)
}

export function formatDate(dateSrc, fmt) {
  if (dateSrc && !isDate(dateSrc) && typeof dateSrc === 'string') {
    dateSrc = dateSrc.replace(/-/g, '\/')
  }
  if (dateSrc && isDate(dateSrc)) {
    const dateObj = new Date(dateSrc);
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (`${dateObj.getFullYear()}`).substr(4 - RegExp.$1.length));
    }
    const o = {
      'M+': dateObj.getMonth() + 1,
      'd+': dateObj.getDate(),
      'h+': dateObj.getHours(),
      'm+': dateObj.getMinutes(),
      's+': dateObj.getSeconds(),
    };
    Object.keys(o).forEach((k) => {
      const str = `${o[k]}`;
      if (new RegExp(`(${k})`).test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : str.padStart(2, '0'));
      }
    });
    return fmt;
  }
  console.log('date error', dateSrc)
  return '';
}

export function isDate(dateSrc) {
  return !!(dateSrc && new Date(dateSrc).toString() !== 'Invalid Date');
}

export function getDateDiff(start, end) {
  const dayTime = 86400000
  let diff = +new Date(end) - +new Date(start)
  diff = diff > 0 ? diff : 0
  return fixedDecimal(diff / dayTime, 1)
}

export function getEnergyPointTimeText(index) {
  const step = 600000
  const baseTime = new Date('1970/01/01 00:00:00').getTime()
  return formatDate(baseTime + index * step, 'hh:mm')
}

export function generateMixed(n) {
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  const length = Math.floor(n) || 0;
  return Array.from({
    length,
  }, () => alphabet[Math.ceil(Math.random() * 25)]).join('');
}

export function getGUID() {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (`${generateMixed(1)}${+new Date()}${generateMixed(2)}${S4() + S4()}${S4()}${S4()}`);
}

export function getFileExt(filePath) {
  if (filePath?.substring) {
    return filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase()
  }
  return ''
}

export function formatUserCode(userId) {
  return `SN${`${userId}`.padStart(6, '0')}`
}

export function getParamByQr(qrString, splitString) {
  const splitKey = `/${splitString}/`
  const paramByQr = qrString.split(splitKey)[1] || ''
  console.log('getParamByQr', qrString, splitKey, paramByQr)
  return paramByQr
}

export function getWeekdaysByIndexArr(weekdays) {
  if (weekdays.length !== 7) return [];
  const weekDay = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
  const arr = [];
  weekdays.split('').forEach((item, index) => {
    if (item === '1') {
      arr.push(weekDay[index]);
    }
  });
  return arr.join('');
}
